<template>
 <div id="home">
   <!-- 头部 -->
   <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <!-- 板块 -->
      <tab-control class="tab-control" 
                     :titles="['流行','新款','精选']" 
                     @tabClick="tabClick" 
                      ref="tabControl1" 
                      v-show="isTabFixed">
      </tab-control>
    <scroll class="content" 
            ref="scroll" 
            :probe-type="3" 
            @scroll="contentScroll" 
            :pull-up-load="true"
             @pullingUp="loadMore">
       <!-- 导航栏 -->
     <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper> 
   <!-- 推荐信息 -->
     <recommend-view :recommends="recommends"></recommend-view>
   <!-- 本周流行 -->
      <feature-view></feature-view>
   <!-- 板块 -->
      <tab-control :titles="['流行','新款','精选']" 
                   @tabClick="tabClick" 
                   ref="tabControl2"></tab-control>
    <!-- 首页商品数据 -->
   <goods-list :goods="showGoods"></goods-list>
    
   </scroll>
   <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
     </div>

</template>

<script>
  //获取 头部
  import NavBar from "@/components/common/navbar/NavBar"
  //获取 导航栏
  import HomeSwiper from './childComps/HomeSwiper'

  //获取 推荐信息
  import recommendView from './childComps/RecommendView'
  //获取 本周流行
  import FeatureView from "./childComps/FeatureView.vue"
 
  //获取 板块
  import TabControl from '@/components/content/mainTabbar/tabControl/TabControl';
  //获取 首页商品数据
  import GoodsList from '@/components/content/goods/GoodsList'
  //获取 数据
  import { getHomeMultidata,getHomeGoods } from "@/network/home"
  //获取 滚动
  import Scroll from '@/components/common/scroll/Scroll'
  //获取 向上跳转
  import BackTop from "@/components/content/backTop/BackTop"; 

  import TabBarItemVue from "@/components/common/tabbar/TabBarItem"

  
 
  export default {
    name: "Home",
    components:{
    NavBar,
    HomeSwiper,
    recommendView,
    FeatureView,
    TabBarItemVue,
    TabControl,
    getHomeGoods,
    GoodsList,
    Scroll,
    BackTop,
},
    data(){
     return{
        banners: [],
        recommends: [],
        //数据板块对象模型
        goods:{
          'pop':{page:0,list: []},
          'new':{page:0,list: []},
          'sell':{page:0,list: []},
        },
        currentType:'pop',
        isShowBackTop:false,
        tabOffsetTop: 0,
        isTabFixed:false,
        saveY: 0,
      }
    },
    
    
    computed: {
      showGoods(){
         return this.goods[this.currentType].list;
      }
    },
    created() {
      // 1.请求多个数据
      this.getHomeMultidata();
      // 2.请求商品数据
     this.getHomeGoods('pop');
     this.getHomeGoods('new');
     this.getHomeGoods('sell');
     
    },
    mounted() {
      
    },
    destroyed() {
      // console.log("销毁");
    },
    activated() {
      // console.log(this.saveY);
    
    this.$refs.scroll.scrollTo(0, this.saveY, 1);
    this.$refs.scroll.refresh();
    },
    deactivated() {
      //console.log('home deactivated')
    this.saveY = this.$refs.scroll.getScrollY();

    // 取消监听
    // this.$bus.$off('itemImgLoad', this.itemImgListener)
    },
    methods: {
      /* 以下为事件监听相关的方法 */
      tabClick(index){
        switch (index){
          case 0:
          this.currentType = 'pop';
          break;
        case 1:
          this.currentType = 'new';
          break;
        case 2:
          this.currentType = 'sell';
          break;
        }
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
      },
      //点击向上跳转按钮
      backClick(){
        this.$refs.scroll.scrollTo(0,0)
      },
      contentScroll(position){
        // 1.判断BackTop是否显示
        this.isShowBackTop = (-position.y) > 1000

        // 2.决定tabControl是否吸顶(position:fixed)
        this.isTabFixed = (-position.y) > this.tabOffsetTop;
      },
      loadMore(){
        // console.log("nihao");
        this.getHomeGoods(this.currentType)
        this.$refs.scroll.scroll.refresh()
      },
      swiperImageLoad(){
        // 2.获取tabControl的offsetTop
      //所有的组件都有一个属性$el:用于获取组件中的元素
     this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
      console.log(this.tabOffsetTop);
      },
      /* 以下为网络请求相关的方法 */
    getHomeMultidata() {
     getHomeMultidata().then(res => {
       console.log(res); 
        this.result = res;
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods(type){
        const page = this.goods[type].page + 1;
          getHomeGoods(type, page).then(res =>{
        //console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        this.$refs.scroll.finishPullUp()
      })
      }
    }
  }
 
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
  .home-nav {
    background-color: var(--color-tint);
    color: #fff;
    font-size: 16px;
    /* position: sticky;
    top: 0;
    z-index: 99; */
  }

 /*  .tab-control {
    position: sticky;
    top: 31px;
    z-index: 99;
  } */
.content {
    overflow: hidden;
    position: absolute;
    top:44px;
    bottom:49px;
    left: 0;
    right: 0;
  }
 /*  .content {
    height:calc(100% - 81px);
    overflow: hidden;
  } */
  
  .tab-control {
    position: relative;
    z-index: 99;
  }
  
</style>
